import Qt 4.7
import "ColumnHelper.js" as ColumnHelper
import "Parsers.js" as Parsers

Item {
    id: routes
    width: 0
    property ListModel legsModel
    property string xml
    property int routeIndex: 0

    onWidthChanged: {
        console.log("MAXMAXMAX/width="+width)
    }

    XmlListModel {
        id: xmlModel
        //        source: "http://api.reittiopas.fi/hsl/prod/?user=zouba&pass=caf9r3ee&request=route&format=xml&language=en&epsg_in=wgs84&epsg_out=wgs84&from=24.7765,60.2046&to=24.8825483159,60.1618275013&show=5&walkspeed=2&optimize=4"
        // source: "routeData.xml"
        xml:  routes.xml
        query: "/response/node"

        XmlRole { name: "depTime"; query: "node/legs/node[1]/locs/node[1]/depTime/string()" }
        XmlRole { name: "code"; query: "node/legs/node[2]/code/string()" }
    }

    ListView {
        id: list
        anchors.fill: parent
//        property variant columnWidths: ColumnHelper.calcColumnWidths(model, list)

        model: xmlModel
        orientation: "Vertical"
        delegate: routeDelegate
    }

    Component {
        id: routeDelegate
        Button {
            id: button
            height: 66
            width: routes.width
            text: {
                var retVal = Parsers.parseTime(model.depTime)+"/"+Parsers.parseCode(model.code)
                var textElement = Qt.createQmlObject('import Qt 4.7; Text { text: "' + retVal + '"}',
                                                     parent, "calcTextWidth");

                var newWidth = textElement.width+40

                if (newWidth>routes.width) {
                    routes.width = newWidth
                }

                // Dispose of temporary element
                textElement.destroy()

                return retVal
            }

            onClicked: {
                console.log("MAXMAXMAX/index="+index)
                routeIndex=index
            }
        }
    }
}
